/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.views;

import com.insurance.cis.controllers.LoginController;
import com.insurance.cis.daos.EmployeeDao;
import com.insurance.cis.models.Employee;
import com.insurance.cis.models.GroupData;
import com.insurance.cis.models.Module;
import com.insurance.cis.models.ModuleData;
import com.insurance.cis.session.UserSession;
import com.insurance.cis.utils.JPanelLoader;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import java.util.List;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author TruongLQ
 */
public class LoginJPanelView extends javax.swing.JPanel {

    final EmployeeDao employeeDao = new EmployeeDao();
    final MainJFrameView mainJFrame;
    final LoginController controller = new LoginController(this);
    /**
     * Creates new form LoginJPanelView
     */
    public LoginJPanelView(javax.swing.JFrame jFrame) {
        initComponents();
        this.mainJFrame = (MainJFrameView)jFrame;
        txtUsername.requestFocus();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        lblChangePassword = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(900, 600));

        jLabel1.setText("User name");

        jLabel2.setText("Password");

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        lblChangePassword.setText("<html><a href=\"\" >Forgot your password?</a></html>");
        lblChangePassword.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblChangePasswordMouseClicked(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Login to CIS application:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(276, 276, 276)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblChangePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnLogin)
                                .addGap(18, 18, 18)
                                .addComponent(btnReset))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(329, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(128, 128, 128)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLogin)
                    .addComponent(btnReset))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblChangePassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(334, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        if(!controller.isValidInputs()) {
            return;
        }
        if (!Utils.checkConnection()) {
            return;
        }
        String username = txtUsername.getText().trim();
        String pass = String.valueOf(txtPassword.getPassword()).trim();
        if (controller.checkEmployee(username,pass)) {
            // go to manage page
            Employee employee = employeeDao.getEmployee(username, pass);
            UserSession.INSTANCE.setCurrentUser(employee);
            mainJFrame.disableAllMenus();
            loadAccessedMenus(employee);
            mainJFrame.loadAllScreens();
            loadDefaultJPanel(employee);
            enableLogoutMenu(true);
        } else {
            MessageDialogUtils.dialogError(this, "User name or password is not correct!");
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void enableLogoutMenu(boolean value) {
        mainJFrame.getMnLogin().setEnabled(!value);
        mainJFrame.getMnLogout().setEnabled(value);
        mainJFrame.getMnDbConfigure().setEnabled(!value);
    }
     
    private void loadAccessedMenus(final Employee employee) {
        mainJFrame.disableAllMenus();
        List<Module> accessedModules = employee.getAccessedModules();
        for (Module module: accessedModules) {
            if (module.getDesc().equals(ModuleData.ACTUAL_COMPAIGN.getDesc())) {
                mainJFrame.getMnCampaign().setEnabled(true);
                mainJFrame.getMnActualCampaign().setEnabled(true);
            } 
            
            if (module.getDesc().equals(ModuleData.ADMIN.getDesc())) {
                mainJFrame.getMnAdministrator().setEnabled(true);
                mainJFrame.getMnEmployeeManagement().setEnabled(true);
            } 
            
            if (module.getDesc().equals(ModuleData.COMPAIGN_MASTER.getDesc())) {
                mainJFrame.getMnCampaignMaster().setEnabled(true);
                mainJFrame.getMnCampaign().setEnabled(true);
            } 
            
            if (module.getDesc().equals(ModuleData.ESTIMATED_COMPAIGN.getDesc())) {
                mainJFrame.getMnEstimatedCampaign().setEnabled(true);
                mainJFrame.getMnCampaign().setEnabled(true);
            } 
            
            if (module.getDesc().equals(ModuleData.HELP.getDesc())) {
                mainJFrame.getMnHelpContents().setEnabled(true);
                mainJFrame.getMnHelpAbout().setEnabled(true);
            } 
            
            if (module.getDesc().equals(ModuleData.PRODUCT_MASTER.getDesc())) {
                mainJFrame.getMnProductMaster().setEnabled(true);
                mainJFrame.getMnProduct().setEnabled(true);
            } 
            
            if (module.getDesc().equals(ModuleData.STATISTICS.getDesc())) {
                mainJFrame.getMnStatistics().setEnabled(true);
                mainJFrame.getMnStatisticsReport().setEnabled(true);
            } 
        }
    }
    
    private void loadDefaultJPanel(final Employee employee) {
        if (GroupData.EMPLOYEE.getDesc().equals(employee.getGroup().getGroupDesc())) {
            JPanelLoader.loadJpanel(mainJFrame.getMainJpnl(), mainJFrame.getStatisticsJpnl());
        } else if (GroupData.COMPAIGN_MASTER.getDesc().equals(employee.getGroup().getGroupDesc())) {
            JPanelLoader.loadJpanel(mainJFrame.getMainJpnl(), mainJFrame.getCampaignMasterJpnl());
        } else if (GroupData.PRODUCT_MASTER.getDesc().equals(employee.getGroup().getGroupDesc())) {
            JPanelLoader.loadJpanel(mainJFrame.getMainJpnl(), mainJFrame.getProductMasterJpnl());
        } else if (GroupData.ADMIN.getDesc().equals(employee.getGroup().getGroupDesc())) {
            JPanelLoader.loadJpanel(mainJFrame.getMainJpnl(), mainJFrame.getAdminJpnl());
        } 
    }
    
    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        txtUsername.setText("");
        txtPassword.setText("");
    }//GEN-LAST:event_btnResetActionPerformed

    private void lblChangePasswordMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblChangePasswordMouseClicked
        mainJFrame.getChangePasswordJpnl().controller.clearChangePasswordJPanel();
        JPanelLoader.loadJpanel(mainJFrame.getMainJpnl(), mainJFrame.getChangePasswordJpnl());
    }//GEN-LAST:event_lblChangePasswordMouseClicked
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblChangePassword;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables

    public JPasswordField getTxtPassword() {
        return txtPassword;
    }

    public void setTxtPassword(JPasswordField txtPassword) {
        this.txtPassword = txtPassword;
    }

    public JTextField getTxtUsername() {
        return txtUsername;
    }

    public void setTxtUsername(JTextField txtUsername) {
        this.txtUsername = txtUsername;
    }

    public void clearAllTextFields() {
        btnResetActionPerformed(null);
    }
}
